-- 解析游戏中的调试命令 

local M = {};
setmetatable (M, {__index = _G;});
_ENV = M;

--[[
* debug(d)  
    show(s) | hide(h) 显示/隐藏游戏日志窗口
* load(l)   
    texture(t) | material(m) | prefab(p) {asset_id} 加载asset_id指向的资源
* print(p)  
    assetdb(a) texture(t) | material(m) | prefab(p) [{asset_id}] 打印资源数据库中的内容，如果asset_id不为空，则只打印asset_id指向的资源信息
    modelpool [{asset_id}] 打印物品池的具体信息，指定asset_id时，只打印和该asset_id关联的信息
* model(g)  
    get(g) {prefab_id} {name} 实例化预设，并起个名字 
    release(r) {name} 释放名为name的物体到池中
    releaseall(ra)  释放所有物体到池中
    destroyfreed(df) 销毁池中的空闲物体
* assetdb(adb)
    unload(u) [texture(t) | material(m) | prefab(p)] 销毁资源数据中引用为0的资源
* gc trigger garbage collection in c#
]]

local game_debug = require "GameDebugToolsLib"

function parse_command (command_line)
    local sub_commands = {};
    for w in string.gmatch (command_line, "[%a_%d]+") do
        table.insert (sub_commands, w);
    end;

    local command = sub_commands[1];
    if (command == "debug" or command == "d") then
        parse_debug_command (sub_commands);
    elseif (command == "load" or command == "l") then
        parse_load_command (sub_commands);
    elseif (command == "print" or command=="p") then
        parse_print_comamnd (sub_commands);
    elseif (command == "gamemodel" or command == "g") then
        parse_gamemodel_command (sub_commands);
    elseif (command == "assetdb" or command == "adb") then
        parse_adb_command (sub_commands);
    elseif (command == "coroutine") then
        require ("tests.test").coroutine_test ();
    elseif (command == "gc") then
        game_debug.GC ();
    end;
end;

function parse_debug_command (command_table) 
    if (command_table[2] == "show" or command_table[2] == "s") then
        game_debug.ShowLogWindow (); 
    elseif (command_table[2] == "hide" or command_table[2] == "h") then
        game_debug.HideLogWindow ();
    end;
end;

function parse_load_command (command_table)
    local asset_id = tonumber (command_table[3]);
    if (command_table[2] == "texture" or command_table[2] == "t") then
        game_debug.LoadTexture (asset_id);
    elseif (command_table[2] == "material" or command_table[2] == "m") then
        game_debug.LoadMaterial (asset_id);
    elseif (command_table[2] == "prefab" or command_table[2] == "p") then
        game_debug.LoadPrefab (asset_id);
    end;
end;

function parse_gamemodel_command (command_table)
    local second_command = command_table[2];
    if (second_command == "get" or second_command == "g") then
       game_debug.GetModel (tonumber (command_table[3]), command_table[4]);
    elseif (second_command == "release" or second_command == "r") then
        game_debug.ReleaseModel (command_table[3]);
    elseif (second_command == "releaseall" or second_command == "ra") then
        game_debug.ReleaseModelAll ();
    elseif (second_command == "destroyfreed" or second_command == "df") then
        game_debug.DestroyModelFreed ();
    end;
end;

function parse_print_comamnd (command_table)
    local target = command_table[2];
    if (target == "assetdb" or target == "adb") then
        target = command_table[3];
        local asset_id = 0;
        if (#command_table > 3) then asset_id = tonumber (command_table[4]); end;
        if (target == "texture" or target == "t") then
            game_debug.PrintTextureInfo (asset_id);        
        elseif (target == "material" or target == "m") then
            game_debug.PrintMaterialInfo (asset_id);
        elseif (target == "prefab" or target == "p") then
            game_debug.PrintPrefabInfo (asset_id);
        end;
    elseif (target == "modelpool" or target == "mp") then
        local asset_id = 0;
        if (#command_table > 2) then
            asset_id = tonumber (command_table[3]);
        end;
        game_debug.PrintModelpoolStatus (asset_id);
    end;
end;

function parse_adb_command (sub_commands)

    if (sub_commands[2] == "unload" or sub_commands[2] == "u") then
        if (#sub_commands == 2) then
            game_debug.UnloadUnusedAssets ();
            return;
        end;
        
        local target = sub_commands[3];
        if (target == "texture" or target == "t") then
            game_debug.UnloadUnusedTextures ();
        elseif (target == "material" or target == "m") then
            game_debug.UnloadUnusedMaterials ();
        elseif (target == "prefab" or target == "p") then
            game_debug.UnloadUnusedPrefabs ();
        end;
    end;

end;

return M;
